package com.zzedu.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.base.Joiner;
import com.google.common.collect.Lists;
import com.zzedu.common.cache.CacheNameContansts;
import com.zzedu.common.utils.StringUtils;
import com.zzedu.common.utils.bean.BeanUtils;
import com.zzedu.domain.ZzAgent;
import com.zzedu.domain.ZzOrganization;
import com.zzedu.dto.ZzOrganizationDTO;
import com.zzedu.mapper.ZzOrganizationMapper;
import com.zzedu.service.IZzAgentService;
import com.zzedu.service.IZzOrganizationService;
import com.zzedu.system.service.ISysRegionService;
import com.zzedu.vo.ZzOrganizationVO;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * 机构信息Service业务层处理
 *
 * @author Mr.Wang
 * @date 2025-02-26
 */
@Service
public class ZzOrganizationServiceImpl extends ServiceImpl<ZzOrganizationMapper, ZzOrganization> implements IZzOrganizationService {
    @Autowired
    ISysRegionService sysRegionService;

    @Autowired
    IZzAgentService zzAgentService;

    @Override
    public LambdaQueryWrapper<ZzOrganization> buildQuery(ZzOrganizationDTO zzOrganization) {
        LambdaQueryWrapper<ZzOrganization> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.like(StringUtils.isNotBlank(zzOrganization.getContacts()), ZzOrganization::getContacts, zzOrganization.getContacts());
        lambdaQueryWrapper.like(StringUtils.isNotBlank(zzOrganization.getOrganizationName()), ZzOrganization::getOrganizationName, zzOrganization.getOrganizationName());
        lambdaQueryWrapper.eq(null != zzOrganization.getState(), ZzOrganization::getState, zzOrganization.getState());
        lambdaQueryWrapper.eq(null != zzOrganization.getStuCount(), ZzOrganization::getStuCount, zzOrganization.getStuCount());
        List<String[]> queryProvinceAndCityAndDistrict = zzOrganization.getQueryProvinceAndCityAndDistrict();
        if (CollectionUtils.isNotEmpty(queryProvinceAndCityAndDistrict)) {
            List<String> province = Lists.newArrayList();
            List<String> city = Lists.newArrayList();
            List<String> district = Lists.newArrayList();
            for (String[] provinceAndCityAndDistrict : queryProvinceAndCityAndDistrict) {
                if (null == provinceAndCityAndDistrict) {
                    continue;
                }
                if (StringUtils.isNotBlank(provinceAndCityAndDistrict[0])) {
                    province.add(provinceAndCityAndDistrict[0]);
                }
                if (provinceAndCityAndDistrict.length > 1 && StringUtils.isNotBlank(provinceAndCityAndDistrict[1])) {
                    city.add(provinceAndCityAndDistrict[1]);
                }
                if (provinceAndCityAndDistrict.length > 2 && StringUtils.isNotBlank(provinceAndCityAndDistrict[2])) {
                    district.add(provinceAndCityAndDistrict[2]);
                }
            }
            lambdaQueryWrapper.in(CollectionUtils.isNotEmpty(province), ZzOrganization::getProvince, province);
            lambdaQueryWrapper.in(CollectionUtils.isNotEmpty(city), ZzOrganization::getCity, city);
            lambdaQueryWrapper.in(CollectionUtils.isNotEmpty(district), ZzOrganization::getDistrict, district);
        }
        lambdaQueryWrapper.orderByDesc(ZzOrganization::getId);
        return lambdaQueryWrapper;
    }

    @Override
    public List<ZzOrganizationVO> convertVO(List<ZzOrganization> records) {
        return Optional.ofNullable(records).orElse(Lists.newArrayList()).stream().map(zzOrganization -> {
            ZzOrganizationVO zzOrganizationVO = new ZzOrganizationVO();
            BeanUtils.copyBeanProp(zzOrganizationVO, zzOrganization);
            List<String> provinceAndCityAndDistrict = sysRegionService.provinceAndCityAndDistrict(Lists.newArrayList(zzOrganization.getProvince(), zzOrganization.getCity(), zzOrganization.getDistrict()));
            if (CollectionUtils.isNotEmpty(provinceAndCityAndDistrict)) {
                zzOrganizationVO.setProvinceAndCityAndDistrict(Joiner.on("/").join(provinceAndCityAndDistrict));
            }
            if (null != zzOrganization.getAgentId()) {
                ZzAgent zzAgent = zzAgentService.getById(zzOrganization.getAgentId());
                if (null != zzAgent) {
                    zzOrganizationVO.setAgentName(zzAgent.getAgentName());
                }
            }
            zzOrganizationVO.setLevel(Lists.newArrayList());
            if(StringUtils.isNotBlank(zzOrganization.getLevel())){
                zzOrganizationVO.setLevel(JSON.parseArray(zzOrganization.getLevel(),String.class));
            }
            return zzOrganizationVO;
        }).collect(Collectors.toList());
    }

    @Override
    @CacheEvict(value = CacheNameContansts.ORG_CACHE,key = "'org_:'+#p0.id")
    public boolean updateById(ZzOrganization entity) {
        return super.updateById(entity);
    }

    @Override
    @Cacheable(value = CacheNameContansts.ORG_CACHE,key = "'org_:'+#p0")
    public ZzOrganization getById(Serializable id) {
        return super.getById(id);
    }
}